<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 分类管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >

                <el-table-column prop="Id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="Name" label="分类名"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"

                        >编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="分类名">
                    <el-input v-model="form.Name"></el-input>
                </el-form-item>


                <el-form-item label="分类图">
                    <el-upload
                            class="avatar-uploader"
                            accept="image/*"
                            action="https://upload-z2.qiniup.com"
                            :data="tokenData"
                            :show-file-list="false"
                            :before-upload="beforeUpload"
                            :on-success="uploadOk">
                        <img v-if="form.Picture" :src="form.Picture" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>



            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'category',
    data() {
        return {
            query: {

            },
            form: {
                Id : 0,
                Name : '',
                Picture : '',
            },
            idx: 0,
            tableData: [],
            editVisible: false,
            tokenData: {token: '', key: ''},
            qiniuUri: ''
        };
    },
    created() {
        this.getData();
        this.http.qiniuToken().then(res => {
            this.tokenData.token = res.data.token
            this.qiniuUri = res.data.uri
        });
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.http.getCats().then(res => {
                this.tableData = res.data ? res.data : [];
            });
        },
        beforeUpload(file) {
           this.tokenData.key = this.getUUID() +  file.name.substring(file.name.lastIndexOf('.'))
        },
        uploadOk(response, file) {
            // 这里是element的上传地址，对应的name,url,自己打印fileList对照
            this.form.Picture = this.qiniuUri + '/' + response.key + '?imageView2/0/w/100/h/100'
        },
        getUUID(){
          let date = new Date();
          let year = (date.getFullYear() + '') . substring(2);        //年 ,从 Date 对象以四位数字返回年份
          let month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
          let day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
          month = (month > 9 ? '' : '0')  + month
          day = (day > 9 ? '' : '0')  + day
          let d = date.getTime();
          return year + month  + day + '-xxxxxxxx-xxxx'.replace(/[xy]/g, function (c) {
            let r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
          });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.form = row;
            this.idx = index
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.http.editCat(this.form.Id, this.form)
            this.$message.success(`修改成功`);
            this.$set(this.tableData, this.idx, this.form);
        },

    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}



.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

</style>
